<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px">
      <el-breadcrumb-item :to="{ path: '/' }">后台管理</el-breadcrumb-item>
      <el-breadcrumb-item>管理员列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-divider></el-divider>

    <el-table
        :data="tableData"
        border
        style="width: 100%">
      <el-table-column prop="id" label="ID" width="50px" align="center"></el-table-column>
      <el-table-column prop="username" label="用户名" width="140px" align="center"></el-table-column>
      <el-table-column prop="nickname" label="昵称" width="140px" align="center"></el-table-column>
      <el-table-column prop="description" label="简介" align="center"></el-table-column>
      <el-table-column prop="phone" label="手机号码" align="center"></el-table-column>
      <el-table-column prop="email" label="电子邮箱" align="center"></el-table-column>
      <el-table-column label="是否启用" align="center" width="70px">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.enable"
                     :active-value="1"
                     :inactive-value="0"
                     @change="changeEnable(scope.row)">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="120px">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" circle size="small"  :disabled="scope.row.id==1" ></el-button>
          <el-button type="danger" icon="el-icon-delete" :disabled="scope.row.id==1"
                     circle size="small" @click="adminDeleteConfirm(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>

  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: []
    }
  },
  methods: {
    changeEnable(admin){
      let enableText = ["禁用, 启用"]
      let url = "http://localhost:9081/admins/"+admin.id;
      if (admin.enable ==1){
        console.log("将启用管理员")
        url += "/enable"
      }else {
        console.log("将禁用管理员")
        url += "/unEnable"
      }
      console.log("url = "+url)
      this.axios.post(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          let message = '将管理员【' + admin.username + '】的启用状态改为【' + enableText[admin.enable] + '】成功！';
          this.$message({
            message: message,
            type: 'success'
          });
        } else {
          this.$message.error(responseBody.message);
        }
        if (responseBody.state == 40400) {
          this.loadAdminList();
        }
      });
    },
    handleAdminDelete(admin){
      console.log("准备删除id为"+admin.id+"相册")
      this.axios.post('http://localhost:9081/admins/'+admin.id+'/delete').then(
          (response)=>{
            let responseBody = response.data;
            if (responseBody.state != 20000){
              this.$message.error(responseBody.message)
            }else{
              this.$message({
                message: '删除成功',
                type: 'success'
              });
            }
            if(responseBody.state==20000 || responseBody.state == 40400){
              this.loadAdminList();
            }
          }
      )

    },
    adminDeleteConfirm(admin){
      let message = '此操作将永久删除'+admin.username+'文件, 是否继续?'
      this.$confirm(message, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleAdminDelete(admin);
      })
    },
    loadAdminList() {
      let url = "http://localhost:9081/admins"
      console.log(url);
      console.log('在localStorage中的JWT数据'+localStorage.getItem('jwt'));
      this.axios
          .create({
            'headers':{
              'Authorization': localStorage.getItem('jwt')
            }
          })
          .get(url).then((response) => {
            let responseBody = response.data;
            this.tableData = responseBody.data;
          }
      )
    }
  },
  mounted() {
    console.log("mounted");
    this.loadAdminList();
  }
}
</script>
